import os
import yaml

from .base import Base
from .page_jumps import PageJumps


class Login(Base):    # 登录页
    __LOGIN_URL = "https://work.weixin.qq.com/wework_admin/frame#contacts"  # url
    root_path = os.path.dirname(os.path.abspath(__file__))  # 获取当前的路径
    dir_path = os.sep.join([root_path, f'../cookies/cookie.yaml'])  # 拼接路径

    def login_by_cookies(self):     # 使用cookie登录
        self.driver.get(self.__LOGIN_URL)
        cookie = self.read_cookies_from_file()
        for c in cookie:
            self.driver.add_cookie(c)
        self.driver.refresh()
        return PageJumps(self.driver)

    def read_cookies_from_file(self):
        with open(f"{self.dir_path}", 'r', encoding="utf-8") as f:
            cookies = yaml.safe_load(f)
            return cookies
